import {Map} from 'immutable';

import actionTypes from './constant';

const isLogin = Boolean(window.sessionStorage.getItem('authToken'));
const user_id = window.sessionStorage.getItem('user_id');
const user_password = window.sessionStorage.getItem('user_password');
const role = window.sessionStorage.getItem('role');


const initState = Map({
    userInfo: {
        user_id:user_id,
        user_password: user_password,
        role: role,
        isLogin: isLogin
    }
})

//进入页面获取token.如果有token，表示已经登录，islogin就为true


function reducer(state = initState, action){
    switch(action.type) {
        case actionTypes.LOGIN_SUCCESS:
            return {
                ...action.payLoad
            };
        case actionTypes.LOGIN_FAILED:
            const newState = {
                user_id: '',
                user_password: '',
                isLogin: false
            }
            return {
                ...state,
                ...newState
            }
        case actionTypes.LOGOUT:
            return {
                ...state,
                isLogin: false
            }
            
        default: 
            return state;
    }
}

export default reducer;